<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class StdToolItemRelController extends Controller
{
    //关联试验项目
    public function relItem(Request $request)
    {
        $id = $request->id; //工器具ID

        if ($request->isMethod('POST')) {
            $data = $request->only(['std_id', 'testitem_id', 'jishu_yaoqiu','excel_pos_id','station_up_param']);
            $data['tool_id'] = $id;
            try {
                $maxSort = (int)DB::table('a_std_tool_item_rel')->where('tool_id', $id)->max('sort');
                $data['sort'] = $maxSort + 3;
                //插入数据    insert 返回true 或 false
                $result = DB::table('a_std_tool_item_rel')->insert($data);
            } catch (\Throwable $e) {
                return '2';
            }
            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result ? '1' : '0';
        }
        else {
            //得到 与当前的工器具关联的所有检验标准的 数组
            $arr_std_id = DB::table('a_std_tool_rel')->where('tool_id', $id)->pluck('std_id');
            $data_std = DB::table('a_standard')->whereIn('id', $arr_std_id)->get();
            //获取当前工器具的信息
            $data_tool = DB::table('a_tool')->find($id);
            //获取所有试验项目的信息
            $data_item = DB::table('a_testitem')->get();

            $sql = "SELECT
                        A.id,
                        A.jishu_yaoqiu,
                        A.pos_start,
                        A.pos_end,
                        A.pos_date,
                        A.pos_tester,
                        A.ishide,
                        A.sort,
                        A.station_up_param,
                        A.station_ids,
                        B.num,
                        C.`name`,
                        D.realname,
                        D.istester,
                        D.`status`,
                        E.`name` as excel_pos_name
                    FROM
                        a_std_tool_item_rel AS A
                        LEFT JOIN a_standard AS B ON A.std_id = B.id
                        LEFT JOIN a_testitem AS C ON A.testitem_id = C.id
                        LEFT JOIN manager AS D ON A.manager_id = D.id
                        LEFT JOIN a_excel_pos AS E ON A.excel_pos_id = E.id
                    WHERE
                        A.tool_id = :id
                    ORDER BY
                        A.sort ASC,
                        A.std_id ASC";

            $data_rel = DB::select($sql, ['id' => $id]);
            return view('admin.stdToolItemRel.relItem', compact('data_std', 'data_tool', 'data_rel', 'data_item'));
        }
    }

    //标准--工器具--项目 关系 修改
    public function edit(Request $request)
    {
        $id = $request->input('id');

        if ($request->isMethod('POST')) {
            $data = $request->only(['ishide', 'jishu_yaoqiu', 'manager_id','excel_pos_id','station_ids']);
            try {
                //update 返回受影响的行数
                $result = DB::table('a_std_tool_item_rel')->where('id', $id)->update($data);
            } catch (\Throwable $e) {
                return '2';
            }

            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result >= 0 ? '1' : '0';
        }
        else {
            //查询信息
            $sql = "SELECT
                        A.jishu_yaoqiu,
                        A.ishide,
                        A.tool_id,
                        A.std_id,
                        A.manager_id,
                        A.excel_pos_id,
                        B.num,
                        C.`name` AS item_name,
                        D.`name` AS tool_name
                    FROM
                        a_std_tool_item_rel AS A
                        LEFT JOIN a_standard AS B ON A.std_id = B.id
                        LEFT JOIN a_testitem AS C ON A.testitem_id = C.id
                        LEFT JOIN a_tool AS D ON A.tool_id = D.id
                    WHERE
                        A.id = :id";

            $info_std_tool_item_rel = DB::select($sql, ['id' => $id]);
            $info_std_tool_item_rel = $info_std_tool_item_rel[0];

            //查询出 状态为启用的 主检人
            $data_tester = DB::table('manager')->where('istester', '1')->where('status', '2')->get();
            //设备编号
            $dev_lists = DB::table('a_station')->whereIn('status', [1, 2])->orderBy('dev_num')->get();

            //获取当前工器具检测标准小的模板
            $std_tool_rel_id = DB::table('a_std_tool_rel')->where(['std_id'=>$info_std_tool_item_rel->std_id,'tool_id'=>$info_std_tool_item_rel->tool_id])->value('id');
            $excel_pos = DB::table('a_excel_pos')->where('std_tool_rel_id', $std_tool_rel_id)->get();
            return view('admin.stdToolItemRel.edit', compact('info_std_tool_item_rel', 'data_tester','excel_pos','dev_lists'));
        }
    }

    //关联标准--项目 删除
    public function del(Request $request)
    {
        $id = $request->id;

        try {
            //  delete 返回受影响的行数
            $result = DB::table('a_std_tool_item_rel')->where('id', $id)->delete();
        } catch (\Throwable $e) {
            return '2';
        }

        //由于框架自身不支持响应bool值，所以转化一种形式
        return $result >= 0 ? '1' : '0';
    }

    /**
     * 排序 标准-工器具-试验项目 关联
     */
    public function saveSortStdToolItemRel(Request $request)
    {
        $params = $request->except('_token','_form_token');
        $updata = [];
        foreach($params as $column=>$arr){
            if ($arr) {
                foreach ($arr as $id => $val) {
                    $val = trim($val);
                    $updata[$id][$column] = $val;
                }
            }
        }
        foreach ($updata as $key => $itemVal) {
            $result = DB::table('a_std_tool_item_rel')->where('id', $key)->update($itemVal);
        }

        return tip("操作成功", 1, true);
        // return back()->withInput();//使用辅助函数  back 返回到前一个 URL
    }

    /**
     * 自动根据ID排序
     */
    public function sort()
    {
        $data = DB::table('a_std_tool_item_rel')->where('sort', 200)->orderBy('id')->get();
        foreach ($data as $item) {
            $maxSort = (int)DB::table('a_std_tool_item_rel')->where('tool_id', $item->tool_id)->max('sort');
            DB::table('a_std_tool_item_rel')->where('id', $item->id)->update(['sort' => $maxSort + 3]);
        }
    }
}
